﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlueLotus7.Common
{
    public class Comment : IValidate
    {
        #region Fields

        protected Guid key;
        protected PersonIdentity personIdentity;
        protected DateTime? createdStamp;
        protected string commentContent;
        protected Guid parentObjectIdentity;
        protected List<Comment> childComments;

        #endregion

        #region Property

        public Guid Key
        {
            get { return key; }
        }

        public PersonIdentity PersonIdentity
        {
            get { return personIdentity; }
        }

        public DateTime? CreatedStamp
        {
            get { return createdStamp; }
            set { createdStamp = value; }
        }

        public string CommentContent
        {
            get { return commentContent; }
            set { commentContent = value; }
        }

        public Guid ParentObjectIdentity
        {
            get { return parentObjectIdentity; }
        }

        public List<Comment> ChildComments
        {
            get { return childComments; }
            set { childComments = value; }
        }

        #endregion

        #region Constructor

        public Comment(string commentContent, PersonIdentity personIdentity, Guid parentObjectIdentity)
        {
            key = Guid.NewGuid();
            this.personIdentity = personIdentity;
            this.parentObjectIdentity = parentObjectIdentity;
            this.commentContent = commentContent;
        }

        public Comment(Guid key, string commentContent, PersonIdentity personIdentity, Guid parentObjectIdentity)
        {
            this.key = key;
            this.personIdentity = personIdentity;
            this.parentObjectIdentity = parentObjectIdentity;
            this.commentContent = commentContent;
        }

        #endregion

        #region IValidate Members

        public bool IsValid
        {
            get
            {
                try
                {
                    Validate();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public void Validate()
        {
            Validator.VerifyNullObject(personIdentity,"personIdentity");
            personIdentity.Validate();

            if (Validator.IsZeroGuid(key))
            {
                throw new InvalidObjectException("key");
            }
            else if (Validator.IsZeroGuid(parentObjectIdentity))
            {
                throw new InvalidObjectException("parentObjectIdentity");
            }
            else if (string.IsNullOrWhiteSpace(commentContent))
            {
                throw new NullReferenceException("commentContent");
            }
        }

        #endregion
    }
}
